package com.ruoyi.system.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.EchartData;
import com.ruoyi.common.core.domain.entity.LineData;
import com.ruoyi.common.core.domain.entity.SysUserCost;
import com.ruoyi.common.core.domain.entity.SysUserCostEchart;
import com.ruoyi.system.mapper.SysUserCostMapper;
import com.ruoyi.system.service.ISysUserCostService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户 业务层处理
 *
 * @author chengyu
 */
@Service
public class SysUserCostServiceImpl implements ISysUserCostService {
    private static final Logger log = LoggerFactory.getLogger(SysUserCostServiceImpl.class);

    @Autowired
    private SysUserCostMapper userCostMapper;

    /**
     * 根据条件分页查询用户列表
     *
     * @param userCost 用户信息
     * @return 用户信息集合信息
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<SysUserCost> selectUserCostList(SysUserCost userCost) {
        return userCostMapper.selectUserCostList(userCost);
    }

    /**
     * pie
     *
     * @param userCostEchart 用户信息
     * @return 用户信息集合信息
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public EchartData selectUserCostPie(SysUserCostEchart userCostEchart) {
        List<SysUserCostEchart> userEchartData = userCostMapper.selectUserCostPie(userCostEchart);
        return convertSqlDataToEchartData(userEchartData);
    }
    /**
     * line
     *
     * @param userCostEchart 用户信息
     * @return 用户信息集合信息
     */
    @Override
//    @DataScope(deptAlias = "d", userAlias = "u")
    public LineData selectUserCostLine(SysUserCostEchart userCostEchart) {
        List<SysUserCostEchart> userEchartData = userCostMapper.selectUserCostLine(userCostEchart);
        return convertSqlDataToLineData(userEchartData);
//        return convertSqlDataToEchartData(userEchartData);
    }

    /**
     * 新增保存用户信息
     *
     * @param userCost 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertUserCost(SysUserCost userCost) {
        // 新增信息
        int rows = userCostMapper.insertUserCost(userCost);
        return rows;
    }

    /**
     * 批量删除用户信息
     *
     * @param userIds 需要删除的用户ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteUserCostByIds(List<String> userIds) {
        return userCostMapper.deleteUserCostByIds(userIds);
    }

    /**
     * 修改用户基本信息
     *
     * @param userCost 用户信息
     * @return 结果
     */
    @Override
    public int updateUserCost(SysUserCost userCost) {
        return userCostMapper.updateUserCost(userCost);
    }


    /**
     * 查看数据信息
     *
     * @param userId 用户信息
     * @return 结果
     */
    @Override
    public SysUserCost selectUserCostById(String userId) {
        SysUserCost userCost = userCostMapper.selectUserCostById(userId);
        System.out.println("User cost found: " + userCost);
        return userCostMapper.selectUserCostById(userId);
    }


    public static EchartData convertSqlDataToEchartData(List<SysUserCostEchart> userEchartData) {
        // 初始化变量来保存每个字段的总和
        double totalBreakfast = 0;
        double totalLunch = 0;
        double totalDinner = 0;
        double totalFruit = 0;
        double totalClothe = 0;
        double totalMother = 0;
        double totalFather = 0;
        double totalOther = 0;
        double totalDailyExpenses = 0;
        double totalEcommerce = 0;
        double totalTraffic = 0;
        double totalAll = 0;

        for (SysUserCostEchart user : userEchartData) {
            totalBreakfast += user.getUserBreakfast();
            totalLunch += user.getUserLunch();
            totalDinner += user.getUserDinner();
            totalFruit += user.getUserFruit();
            totalClothe += user.getUserClothe();
            totalMother += user.getUserMother();
            totalFather += user.getUserFather();
            totalOther += user.getUserOther();
            totalDailyExpenses += user.getUserDailyExpenses();
            totalEcommerce += user.getUserEcommerce();
            totalTraffic += user.getUserTraffic();
        }
        totalAll = totalBreakfast + totalLunch + totalDinner + totalFruit + totalClothe + totalMother + totalFather + totalOther + totalDailyExpenses + totalEcommerce + totalTraffic;
        EchartData echartData = new EchartData();
        echartData.addDataItem("早餐", String.format("%.2f", totalBreakfast));
        echartData.addDataItem("午餐", String.format("%.2f", totalLunch));
        echartData.addDataItem("晚餐", String.format("%.2f", totalDinner));
        echartData.addDataItem("水果", String.format("%.2f", totalFruit));
        echartData.addDataItem("衣物", String.format("%.2f", totalClothe));
        echartData.addDataItem("日用", String.format("%.2f", totalDailyExpenses));
        echartData.addDataItem("电商", String.format("%.2f", totalEcommerce));
        echartData.addDataItem("交通", String.format("%.2f", totalTraffic));
        echartData.addDataItem("妈妈", String.format("%.2f", totalMother));
        echartData.addDataItem("爸爸", String.format("%.2f", totalFather));
        echartData.addDataItem("其他", String.format("%.2f", totalOther));
        echartData.setTotal(String.format("%.2f", totalAll));
        return echartData;
    }
    public static LineData convertSqlDataToLineData(List<SysUserCostEchart> userEchartData) {
        // 初始化变量来保存每个字段的总和
        LineData lineData = new LineData();
        for (SysUserCostEchart user : userEchartData) {
            lineData.addData(user.getUserRecordTime().substring(0, 10), user.getUserLineTotal());
        }
        return lineData;
    }
}
